package com.contract.controller;

import com.contract.pojo.dto.ContractViewDto;
import com.contract.pojo.input.ContractInvalidInput;
import com.contract.pojo.input.ContractQueryInput;
import com.contract.pojo.input.ContractSaveInput;
import com.contract.pojo.input.ContractSignInput;
import com.contract.pojo.output.JsonResult;
import com.contract.pojo.output.PageOutput;
import com.contract.service.BizContractInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 合同controller
 *
 * @author liw
 * @version 1.0
 * @date 2022/8/13 11:10
 */
@ApiOperation("合同")
@RestController
@RequestMapping("contract/contract")
public class BizContractInfoController {

    @Autowired
    private BizContractInfoService bizContractInfoService;

    @PostMapping("info")
    @ApiOperation("新增")
    @PreAuthorize("hasAuthority('contract_save')")
    public JsonResult<Long> save(@RequestHeader("token") String token, @Valid @RequestBody ContractSaveInput vo) {
        return bizContractInfoService.save(vo);
    }

    @GetMapping("list")
    @ApiOperation("查询列表")
    @PreAuthorize("hasAuthority('contract_list')")
    public JsonResult<PageOutput<ContractViewDto>> queryList(@RequestHeader("token") String token, ContractQueryInput vo) {
        return bizContractInfoService.queryList(vo);
    }

    @PostMapping("invalid")
    @ApiOperation("失效")
    @PreAuthorize("hasAuthority('contract_invalid')")
    public JsonResult<Long> invalid(@RequestHeader("token") String token, @Valid @RequestBody ContractInvalidInput vo) {
        return bizContractInfoService.invalid(vo);
    }

    @PostMapping("stamp/{id}")
    @ApiOperation("公司签章")
    @PreAuthorize("hasAuthority('contract_stamp')")
    public JsonResult<Long> stamp(@RequestHeader("token") String token, @PathVariable("id") Long id) {
        return bizContractInfoService.stamp(id);
    }

    @PostMapping("sign")
    @ApiOperation("员工签字")
    @PreAuthorize("hasAuthority('contract_sign')")
    public JsonResult<Long> sign(@RequestHeader("token") String token, @Valid @RequestBody ContractSignInput vo) {
        return bizContractInfoService.sign(vo);
    }

    @GetMapping("preview/{id}")
    @ApiOperation("合同模板测试")
    @PreAuthorize("hasAuthority('contract_preview')")
    public void preview(@RequestHeader("token") String token, @PathVariable("id") Long id) {
        bizContractInfoService.preview(id);
    }

    @DeleteMapping("info/{id}")
    @ApiOperation("删除合同")
    @PreAuthorize("hasAuthority('contract_delete')")
    public JsonResult<Long> delete(@RequestHeader("token") String token, @PathVariable("id") Long id) {
        return bizContractInfoService.delete(id);
    }

}
